package com.cskaoyan.controller.wx.service;

import com.cskaoyan.bean.bo.PageCommonParamBo;
import com.cskaoyan.bean.bo.TopicCommonBo;
import com.cskaoyan.bean.bo.TopicDetailBo;
import com.cskaoyan.bean.entity.Goods;
import com.cskaoyan.bean.entity.Topic;
import com.cskaoyan.bean.entity.TopicExample;
import com.cskaoyan.bean.vo.BasePageData;
import com.cskaoyan.common.util.BeansCopyUtils;
import com.cskaoyan.common.util.ObjectUtils;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.TopicMapper;
import com.cskaoyan.utils.LimitListSizeUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <h2> 专题精选 TopicServiceImpl </h2>
 * Created by GaoJia
 * Date 2021/10/22 09:41
 */
@Service
public class TopicServiceImpl  implements TopicService {

    @Autowired
    TopicMapper topicMapper;

    @Autowired
    GoodsMapper goodsMapper;

    /**
     * <h2> 专题 相关商品 </h2>
     */
    @Override
    public BasePageData<Topic> related(Integer id) {

        // 这里分页结果是写死的。 获取 Example 进行条件搜索 返回分页结果
        PageHelper.startPage(1,4);
        List<Topic> topics = topicMapper.selectByExample(null);
//        List<Topic> topics1 = topicMapper.selectByExample(new TopicExample());
        return BasePageData.ok(topics);
    }

    /**
     * <h2> 专题 列表 </h2>
     */
    @Override
    public BasePageData list(PageCommonParamBo pageParam) {

        // 获取 Example 进行条件搜索 返回分页结果
        PageHelper.startPage(pageParam.getPage(), pageParam.getLimit());
        List<Topic> topics = topicMapper.selectByExample(null);
//        List<Topic> topics1 = topicMapper.selectByExample(new TopicExample());
        BasePageData basePageData = BasePageData.ok(topics);
        List list = basePageData.getList();

        // 使用 BeanUtils 进行 List<Topic> 数据转换 List<TopicCommonBo>
        List<TopicCommonBo> topicCommonBos = BeansCopyUtils.copyList(list, TopicCommonBo.class);
        basePageData.setList(topicCommonBos);
        return basePageData;
    }

    /**
     * <h2> 专题 详情 </h2>
     */
    @Override
    public TopicDetailBo detail(Integer id) {

        // 显示一个不需要 分页的单一对象
        Topic topic = topicMapper.selectByPrimaryKey(id);
        TopicDetailBo topicDetailBo = new TopicDetailBo();
        topicDetailBo.setTopic(topic);


        String[] goods = topic.getGoods();
        // 判空
        if (ObjectUtils.isEmpty(goods)) {
            topicDetailBo.setGoods(null);
            return topicDetailBo;
        }

        // TODO login 之后 这两行 sout 可以去掉
        System.out.println(goods[0]);
        int goodsId = Integer.parseInt(goods[0]);
        System.out.println(goodsId);
        // 根据 goodsId 获取 goods
        Goods goods1 = goodsMapper.selectByPrimaryKey(goodsId);

        topicDetailBo.setGoods((List<Goods>) goods1);

        return topicDetailBo;
    }


    /**
     *  获取所有的专题列表
     *
     * @return
     */
    @Override
    public List<Topic> getAllTopics() {
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria topicExampleCriteria = topicExample.createCriteria();
        // 按照专题阅读量降序排列
        // 筛选没删除的
        topicExampleCriteria.andDeletedEqualTo(false);
        topicExample.setOrderByClause("read_count desc");
        List<Topic> topicList = topicMapper.selectByExample(topicExample);
        return LimitListSizeUtil.limitSize(topicList);
    }
}
